/*
 * channleManager.h
 *
 *  Created on: Mar 8, 2011
 *      Author: B. Jones
 */

#ifndef CHANNLEMANAGER_H_
#define CHANNLEMANAGER_H_

#include "theEncryptor.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <netdb.h>
#include <cstring>
#include <arpa/inet.h>
#include "listener.h"
#include <pthread.h>



class channelManager
{
public:
	channelManager();
	~channelManager();

	static void sendMessage(std::string str,int link_id);
	static void parseChatReception(int connection,const char* buffer);
	void startThreads(int link_id);
	void initiateFileTransfer(int link_id, const char* filePath, const char* filename);
	void initiateFileReceive(int link_id, const char* savePath,bool yesno=true);
	static void *send_fileT(void *args);
	static void *receive_fileT(void *args);
	//int length(const char* temp);
	//void initiateFileReceive(int sockFile_receive)

	struct accessHelper
	{
		channelManager* This;
		void* actual_arg ;
		int link_id;
		accessHelper( channelManager* t, void* p,int link_id)
		: This(t), actual_arg(p),link_id(link_id){}
	};

	struct rtd
	{
		channelManager* This;
		void* actual_arg ;
		int link_id;
		std::string fname;
		std::string absolutePath;
		bool yesno;
		rtd( channelManager* t, void* p, int link_id,
				std::string fname,std::string absolutePath,bool yesno)
		: This(t), actual_arg(p),link_id(link_id),
		 fname(fname),absolutePath(absolutePath),yesno(yesno){}
	};

	int pid;

	std::string receiveString;

	static pthread_mutex_t sendMutex;

	// data receive threads
	// note that fileReceive is for protocol only
	static void *chatReceive(void *args);
	static void *fileReceive(void *args);

};


#endif /* CHANNLEMANAGER_H_ */
